<template>
  <form-create
    v-model="formData"
    v-model:api="fapi"
    :rule="rules"
    :option="options"
    @submit="onSubmit"
  />
</template>

<script>
import { FormApi } from '@/api/system/form'
import formCreate from '@form-create/element-ui'
import { defineEmits } from 'vue'

export default {
  components: { // 使用 components 来注册组件
    formCreate: formCreate.$form()
  },
  props: { // 第二种方式
    option: {
      type: String,
      required: true
    },
    rule: {
      type: String,
      required: true
    },
    taskId: {
      type: String,
      required: true
    },
    formId: {
      type: String,
      required: true
    },
    releaseVersion: {
      type: String,
      required: true
    }
  },
  setup () {
    const emit = defineEmits(['sendData'])
    return { emit }
  },
  data () {
    const option = formCreate.parseJson(this.option)
    return {
      formData: {},
      fapi: null,
      rules: formCreate.parseJson(this.rule),
      options: option,
      submitData: {}
    }
  },
  methods: {
    async onSubmit (formData) {
      this.submitData.dffTaskId = this.taskId
      this.submitData.dffFormId = this.formId
      this.submitData.dffFormVersion = this.releaseVersion
      this.submitData.dffFillVersion = 1
      this.submitData.dffFillStatus = 1
      this.submitData.dffFillWay = 1
      this.submitData.dffFillData = JSON.stringify(formData)
      await FormApi.submitFill(this.submitData)
      
      this.$emit('sendData', 'common.createSuccess')
    }
  }
}
</script>